﻿Imports BusinessObject
Imports DataObject
Imports System.ComponentModel
Public Class frmBaseCategoryDetail

#Region "Variable"

    Public Enum EditMode As Integer
        Edit = 0
        AddNew = 1
        QuickAdd = 2
        Delete = 3
        None = 4
        [Select] = 5
    End Enum

    Protected oRow As DataRow
    Protected eActionMode As EditMode
    Protected sTableName As String 'Biến lưu tên Table của gdsMisaMimosa được dùng
    Protected sKeyNameID As String ' Tên trường khóa của bảng
    Protected sKeyNameAlias As String 'Tên trường Alias của bảng
    Protected sOldAlias As String = "" ' Lưu lại giá trị Alias trước khi sửa
    Protected oBindingSource As BindingSource
    Protected oDictionary As hDBase
    Private sCaption As String

#End Region

#Region "Property"
    Public Property DictionaryObject() As hDBase
        Get
            Return oDictionary
        End Get
        Set(ByVal value As hDBase)
            oDictionary = value
        End Set
    End Property
    Public Property BindingSource() As BindingSource
        Get
            Return oBindingSource
        End Get
        Set(ByVal value As BindingSource)
            oBindingSource = value
        End Set
    End Property
    Public Property RowDetail() As DataRow
        Get
            Return oRow
        End Get
        Set(ByVal Value As DataRow)
            oRow = Value
        End Set
    End Property

    <Description("The name of the dictionary table"), Category("Treaty")> _
    Public Property TableName() As String
        Get
            Return sTableName
        End Get
        Set(ByVal Value As String)
            sTableName = Value
        End Set
    End Property

    <Description("The primary key of the dictionary table"), Category("Treaty")> _
        Public Property TablePrimaryKey() As String
        Get
            Return sKeyNameID
        End Get
        Set(ByVal Value As String)
            sKeyNameID = Value
        End Set
    End Property

    <Description("The Field name stored Alias of the dictionary"), Category("Treaty")> _
        Public Property KeyNameAlias() As String
        Get
            Return sKeyNameAlias
        End Get
        Set(ByVal Value As String)
            sKeyNameAlias = Value
        End Set
    End Property

    Public Property ActionMode() As EditMode
        Get
            Return eActionMode
        End Get
        Set(ByVal value As EditMode)
            eActionMode = value
        End Set
    End Property

#End Region

    Public Overridable Sub LoadInitialData()

    End Sub

    Private Sub frmBaseCategoryDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadInitialData()
    End Sub
    ''' <summary>
    ''' Hàm thực hiện việc binding dữ liệu từ rows dữ liệu đã lấy được từ form list sang các control ở form detail (textbox, checkbox...)
    ''' </summary>
    ''' <param name="oContainer"></param>
    ''' <remarks></remarks>
    Protected Overridable Sub DataBindingToControl(ByVal oContainer As Control)
        Dim myControl As Control
        Dim strTag As String = Nothing
        If Not IsNothing(oBindingSource) Then
            For Each myControl In oContainer.Controls
                strTag = CStr(myControl.Tag)
                If Not IsNothing(strTag) AndAlso strTag <> "" Then
                    If (myControl.GetType Is GetType(Infragistics.Win.UltraWinEditors.UltraTextEditor)) OrElse (myControl.GetType Is GetType(Infragistics.Win.Misc.UltraLabel)) Then
                        myControl.DataBindings.Add("Text", oBindingSource, strTag)
                    Else
                        If Not IsNothing(myControl.GetType.GetProperty("Checked")) Then
                            myControl.DataBindings.Add("Checked", oBindingSource, strTag)
                        Else
                            myControl.DataBindings.Add("Value", oBindingSource, strTag)
                        End If
                    End If
                Else
                    If myControl.Controls.Count > 0 Then
                        DataBindingToControl(myControl)
                    End If
                End If
            Next
        End If
    End Sub

    Protected Overridable Function MustSaveData() As Boolean
        Dim blnResult As Boolean = False
        If (Not IsNothing(oBindingSource)) Then
            oBindingSource.EndEdit()
            blnResult = Not IsNothing(oRow.Table.GetChanges)
        End If
        Return blnResult
    End Function

    Protected Overridable Function SaveDictionary() As Boolean
        Return True
    End Function

    Public Overridable Function ValidateData() As Boolean
        Return True
    End Function

    Protected Overridable Function PreSaveData() As Boolean
        Dim bResult As Boolean = False
        Try
            If Me.Validate AndAlso ValidateData() Then
                If MustSaveData() Then
                    bResult = SaveDictionary()
                Else
                    bResult = True
                End If
            End If
            If bResult = False Then
                'MessageBox.Show("Thao tác cập nhật dữ liệu chưa thực hiện được", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return bResult
    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If PreSaveData() Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub frmBaseCategoryDetail_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MustSaveData() Then
        Else
            oBindingSource = Nothing
        End If
    End Sub

End Class